<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
<script>
    /*     数组元素去重  使用indexOf方法  */
    //indexOf 判断下标即是当前值的下标就符合要求

    let arr = [1, 2, 2, 4, 5, 5, 5, 6, 9, 9]

    arr = arr.filter((item, index) => {
        return arr.indexOf(item) === index
    })

    console.log(arr);

    /*     数组元素去重   使用Array.from方法 */
    Array.from(arr)
    console.log(arr);

    /*    根据某属性对数组对象去重，相同只保留1个   使用for判断  */
    let str = [{
        id: 1,
        name: 'jack',
    }, {
        id: 1,
        name: 'james',
    }, {
        id: 2,
        name: 'marry',
    }, {
        id: 2,
        name: 'ben',
    }, {
        id: 3,
        name: 'kitty',
    }]

    let newArr = [];
    let obj = {};
    for (var i = 0; i < str.length; i++) {
        if (!obj[str[i].id]) {
            newArr.push(str[i]);
            obj[str[i].id] = true;
        }
        console.log(i, obj)
    }
    console.log(newArr)
</script>
</body>

</html>
